<template>
	<view class="container">
		<button :disabled="startBtndisabled" @click="startRecord">开始录音</button>
		<button :disabled="endBtndisabled" @click="endRecord">结束录音</button>
		<button @click="playRecord">播放录音</button>
		<button @click="uploadRecord">上传录音</button>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				startBtndisabled:false,
				endBtndisabled:true,
				//录音管理器
				recordManager:null,
				//音频播放组件
				innerAudioContext : null,
				//录音文件地址
				tempFilePath:null
				
			};
		},
		methods:{
			//开始录音
			startRecord(){
				this.recordManager.start(
				{
					format:'mp3',
					duration: 300000
				})
				
			},
			endRecord(){
				this.recordManager.stop()
				
			},
			playRecord(){
				console.log("sss")
				this.innerAudioContext.src=this.tempFilePath;
				this.innerAudioContext.play();
			},
			uploadRecord()
			{
				uni.uploadFile({
					url:'http://localhost:8080/interview/answer/testUploadRecord',
					filePath:this.tempFilePath,
					name:'file',
					formData:{
						questionId: 10
						
					},
					success(res)
					{
						console.log("ssss")
						console.log('res',res)
					}
				})
			}
		},
		onLoad(){
			this.recordManager = uni.getRecorderManager();
			
			this.innerAudioContext = uni.createInnerAudioContext();
			//开始的时候
			this.recordManager.onStart(()=>{
				this.startBtndisable=true
				this.endBtndisabled=false
				console.log("开始录音.......")
			});
			//结束的时候
			this.recordManager.onStop((res)=>
			{
				this.startBtndisable=false
				this.endBtndisabled = true
				console.log("结束录音.......")
				console.log("tempFilePath",res)
				this.tempFilePath=res.tempFilePath
			})
			
		}
	}
</script>

<style lang="scss">

</style>